// 弹窗信息内容
import React from 'react';
import { connect } from 'dva';
import moment from 'moment';
import { FormGroup, DATA_ENTRY, codeType } from '../../../../comps';
import { formSelectData } from '../../../../utils/utils';

class MyModal extends React.Component {
  constructor() {
    super();
    this.state = {};
  }

  componentDidMount() {
    this.props.onRef(this);
   if( this.props.detailData?.timeStamp) this.setState({
    timeStamp:this.props.detailData?.timeStamp
    })
  }

  UNSAFE_componentWillReceiveProps(nextProps) {
    if (nextProps.detailData?.timeStamp !=this.state.timeStamp) {
      this.setState({
        timeStamp:nextProps.detailData?.timeStamp 
      })
      if (nextProps.detailData?.counterpartyId) {
        // 如果编辑有交易对手，则需要单独设置下对手的值，并告知组件是需要回填；之后还需要重置为正常查询列表的，两个不是一个接口
        this.setState(
          {
            scenesType: '3',
          }, 
          () => {
            this.tradingCompany.setSelect(nextProps.detailData?.counterpartyId);
            this.setState({
              scenesType: undefined,
            });
          },
        );
      }
    }
  }

  getInfo = () => {
    const values = this.state.myFormRef?.getFormGroupData();
    
    if (values) {
      values.investmentPortfo = this.state.investmentPortfo||this.props.detailData?.investmentPortfo;
      values.investmentPortfoId = this.state.investmentPortfoId||this.props.detailData?.investmentPortfoId;
    }
    return values;
  };

  // 交易类型变更
  typeChange = (e) => {
    this.setState({
      counterpartyType: e,
    });
  };

  // 投资组合变更，保存对应的组合名称
  onChangePort = (e) => {
    this.setState({
      investmentPortfo: e?.info.portfolioCode,
      investmentPortfoId: e?.key
    });
  };

  // 清空表单
  reset = () => {
    this.state.myFormRef?.props?.form?.resetFields();
    this.setState({
      counterpartyType: undefined, // 清空交易对手标志位
      investmentPortfo: undefined, // 清空组合
      investmentPortfoId:undefined,
    });
    // 清空交易对手的下拉框数据
    this.tradingCompany.setSelect();
  };

  render() {
    const {
      globalM: {
        dataDictionary: { COUNTERPARTY_TYPE = [] },
      },
      detailData = {},
    } = this.props;
    const formData = (type) => {
      return !(type > 6 && type < 17);
    };
    const flag = formData(this.state.counterpartyType ?? detailData?.counterpartyType);
    return (
      <div>
        <FormGroup
          onRef={(ref) => (this.state.myFormRef = ref)}
          resultIncludeNull={true}
          widthSet={1000}
          dataSource={[
            {
              dataIndex: 'investmentPortfo',
              title: '投资组合',
              type: DATA_ENTRY.PRODUCTGROUPTREE,
              initialValue: detailData.investmentPortfoId,
              span: 12,
              rules: [{ required: true, message: '请选择投资组合!' }],
              multiple: false,
              onChange: this.onChangePort,
            },
            {
              dataIndex: 'repurchaseCode',
              title: '回购代码',
              type: DATA_ENTRY.INPUT,
              initialValue: detailData.repurchaseCode,
              span: 12,
              rules: [{ required: true, message: '请输入回购代码!' }],
            },
            {
              title: '回购方向',
              dataIndex: 'repurchaseDirection',
              type: DATA_ENTRY.SELECT,
              dataSource: this.props.direction || [],
              initialValue: detailData.repurchaseDirection,
              span: 12,
              rules: [{ required: true, message: '请选择回购方向!' }],
            },
            {
              title: '回购起息日',
              dataIndex: 'startTime',
              type: DATA_ENTRY.SINGLEDATE,
              initialValue: detailData.startTime ? moment(detailData.startTime) : undefined,
              span: 12,
              rules: [{ required: true, message: '请选择回购起息日!' }],
            },
            {
              title: '回购到期日',
              dataIndex: 'endTime',
              type: DATA_ENTRY.SINGLEDATE,
              initialValue: detailData.endTime ? moment(detailData.endTime) : undefined,
              span: 12,
              rules: [{ required: true, message: '请选择回购到期日!' }],
            },
            {
              title: '回购金额',
              dataIndex: 'repurchaseMoney',
              type: DATA_ENTRY.INPUTNUMBER,
              initialValue: detailData.repurchaseMoney,
              span: 12,
              unit: '元',
              rules: [{ required: true, message: '请输入回购金额!' }],
            },
            {
              title: '回购利率',
              dataIndex: 'repurchaseRate',
              type: DATA_ENTRY.INPUTNUMBER,
              initialValue: detailData.repurchaseRate ? parseFloat(detailData.repurchaseRate * 100).toFixed(2) : undefined,
              precision: 2,
              unit: '%',
              span: 12,
              rules: [{ required: true, message: '请输入回购利率!' }],
            },
            {
              title: '费用',
              dataIndex: 'fee',
              type: DATA_ENTRY.INPUTNUMBER,
              initialValue: detailData.fee,
              unit: '元',
              span: 12,
            },
            {
              title: '交易场所',
              dataIndex: 'tradingPlace',
              type: DATA_ENTRY.TRADINGMARKET,
              initialValue: detailData.tradingPlace,
              marketType: ['jys', 'yhj'],
              span: 12,
              rules: [{ required: true, message: '请选择交易场所!' }],
            },
            {
              title: '交易对手',
              dataIndex: 'counterpartyId',
              type: DATA_ENTRY.TRADINGCOMPANY,
              span: 12,
              scenesType: this.state.scenesType,
              noQuery:detailData.counterpartyId,
              onRef: (ref) => (this.tradingCompany = ref),
            },
            {
              title: '交易对手类型',
              dataIndex: 'counterpartyType',
              type: DATA_ENTRY.SELECT,
              initialValue: detailData.counterpartyType,
              dataSource: formSelectData(COUNTERPARTY_TYPE),
              span: 12,
              allowClear:true,
              // rules: [{ required: true, message: '请选择交易对手类型!' }],
              onChange: this.typeChange,
              positionBody: true,
            },
            {
              dataIndex: 'counterpartyProductCode',
              title: '交易对手产品代码',
              type: DATA_ENTRY.INPUT,
              initialValue: detailData.counterpartyProductCode,
              span: 12,
              // rules: [{ required: true, message: '请选择交易对手产品代码!' }],
              hide: flag,
            },
            {
              dataIndex: 'counterpartyCompanyCode',
              title: '交易对手机构编码',
              type: DATA_ENTRY.INPUT,
              initialValue: detailData.counterpartyCompanyCode,
              span: 12,
              // rules: [{ required: true, message: '请选择交易对手机构编码!' }],
              hide: flag,
            },
            {
              dataIndex: 'doneCode',
              title: '成交编码',
              type: DATA_ENTRY.INPUT,
              initialValue: detailData.doneNo,
              span: 12,
              // rules: [{ required: true, message: '请选择成交编码!' }],
            },
          ]}
          autoLayout={false}
        />
      </div>
    );
  }
}
export default connect(({ globalM }) => ({ globalM }))(MyModal);
